package com.duke.shaking.more;


import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.duke.shaking.R;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.loopj.android.http.AsyncHttpClient;

public class BuyServiceActivity extends TitleActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				super.onResultSuccess(response);
				JSONObject jsonObj = this.getBody(response);
				if (jsonObj != null) {
					try {
						int coins = jsonObj.getInt(NetUtils.ACCOUNT);
						if (coins < 30) {
							coins = 30 - coins;
						}
						else {
							coins = 0;
						}
						showCoins(coins);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					}
				}
			}

			@Override
			public void onResultFailure(String result) {
				super.onResultFailure(result);
				
			}
        };
        
        AsyncHttpClient client = NetUtils.getAccountInfo(this, responseHandler);
        showProgress(R.string.progress_get_account, client, responseHandler);
	}
	
	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_vip_buy_coins;
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				0,
				0,
				R.string.title_buy_vip_service);
	}
	
	public void goBuyCoins(View v) {
		go(AccountActivity.class);
	}
	
	private void showCoins(int coins) {
		TextView txtCoins = (TextView)this.findViewById(R.id.txt_coins);
		if (txtCoins != null) {
			txtCoins.setText(String.valueOf(coins));
		}
	}

}
